namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineGannGrid : PaintLineBase
    {
        public PaintLineGannGrid()
        {
            base.LineType = PaintLineType.GannGrid;
            base.PlName_zh = "江恩网格";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                PaintLineBase.PointValue value3 = base.PointList[1];
                int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num4 = base.CgInstance.GetX(num2);
                PaintLineBase.PointValue value4 = base.PointList[0];
                float num5 = base.CgInstance.GetY(base.OwnedPanelID, value4.Value, null);
                float num6 = num5 - (num4 - x);
                if (((crossHairPoint.X >= (x - (2.5f * base.LineWidth))) && (crossHairPoint.X <= (x + (2.5f * base.LineWidth)))) && ((crossHairPoint.Y >= (num5 - (2.5f * base.LineWidth))) && (crossHairPoint.Y <= (num5 + (2.5f * base.LineWidth)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeNS;
                    base.MoveType = 1;
                }
                else if (((crossHairPoint.X >= (num4 - (2.5f * base.LineWidth))) && (crossHairPoint.X <= (num4 + (2.5f * base.LineWidth)))) && ((crossHairPoint.Y >= (num6 - (2.5f * base.LineWidth))) && (crossHairPoint.Y <= (num6 + (2.5f * base.LineWidth)))))
                {
                    base.CgInstance.Cursor = Cursors.SizeWE;
                    base.MoveType = 2;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                PaintLineBase.PointValue value2 = pList[0];
                double chartValue = value2.Value;
                PaintLineBase.PointValue value3 = pList[1];
                double num1 = value3.Value;
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num3 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float num4 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num5 = base.CgInstance.GetX(num3) - base.CgInstance.LeftYScaleWidth;
                float num6 = (base.CgInstance.GetY(base.OwnedPanelID, chartValue, null) - div.Rect.Y) - div.TitleHeight;
                float num7 = num6 - (num5 - num4);
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                double[] param = Common.GetLineParam(num4, num6, num5, num7);
                if (param != null)
                {
                    GraphicsPath path = this.GetGP(num4, num6, num5, num7, param);
                    g.DrawPath(pen, path);
                    path.Dispose();
                }
                param = Common.GetLineParam(num4, num6, num4 - (num5 - num4), num7);
                if (param != null)
                {
                    GraphicsPath path2 = this.GetGP(num4, num6, num5, num7, param);
                    g.DrawPath(pen, path2);
                    path2.Dispose();
                }
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num4 - (base.LineWidth * 2.5f), num6 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num5 - (base.LineWidth * 2.5f), num7 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                pen.Dispose();
                brush.Dispose();
            }
        }

        public GraphicsPath GetGP(float x1, float y1, float x2, float y2, double[] param)
        {
            if (param == null)
            {
                return null;
            }
            GraphicsPath path = new GraphicsPath();
            List<PointF> list = this.GetPointList(x1, y1, x2, y2, param);
            for (int i = 0; i < list.Count; i++)
            {
                if (((i + 1) % 2) == 0)
                {
                    PointF tf = list[i - 1];
                    PointF tf2 = list[i];
                    path.AddLine(tf, tf2);
                    path.CloseFigure();
                }
            }
            double num2 = param[0];
            double num3 = param[1];
            float p = (float) num3;
            float num5 = (float) ((num2 * base.CgInstance.GetWorkSpaceX()) + num3);
            float num6 = (float) (-num3 / num2);
            float num7 = (float) ((base.CgInstance.GetWorkSpaceY(base.OwnedPanelID) - num3) / num2);
            List<float> list2 = this.GetVisiblePoint(num6, 0f, base.CgInstance.GetWorkSpaceX(), (x2 - x1) * 2f);
            List<float> list3 = this.GetVisiblePoint(p, 0f, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID), (x2 - x1) * 2f);
            for (int j = 0; j < list2.Count; j++)
            {
                path.AddLine(list2[j], 0f, list2[j] + (num7 - num6), (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID));
                path.CloseFigure();
            }
            for (int k = 0; k < list3.Count; k++)
            {
                path.AddLine(0f, list3[k], base.CgInstance.GetWorkSpaceX(), list3[k] + (num5 - p));
                path.CloseFigure();
            }
            List<float> list4 = this.GetVisiblePoint(num7, 0f, base.CgInstance.GetWorkSpaceX(), (x2 - x1) * 2f);
            List<float> list5 = this.GetVisiblePoint(num5, 0f, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID), (x2 - x1) * 2f);
            for (int m = 0; m < list4.Count; m++)
            {
                if (!list2.Contains(list4[m] - (num7 - num6)))
                {
                    path.AddLine(list4[m], (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID), list4[m] - (num7 - num6), 0f);
                    path.CloseFigure();
                }
            }
            for (int n = 0; n < list5.Count; n++)
            {
                if (!list3.Contains(list5[n] - (num5 - p)))
                {
                    path.AddLine(base.CgInstance.GetWorkSpaceX(), list5[n], 0f, list5[n] - (num5 - p));
                    path.CloseFigure();
                }
            }
            return path;
        }

        public List<PointF> GetPointList(float x1, float y1, float x2, float y2, double[] param)
        {
            List<PointF> list = new List<PointF>();
            double num = param[0];
            double num2 = param[1];
            float p = (float) num2;
            float num4 = (float) ((num * base.CgInstance.GetWorkSpaceX()) + num2);
            float num5 = (float) (-num2 / num);
            float num6 = (float) ((base.CgInstance.GetWorkSpaceY(base.OwnedPanelID) - num2) / num);
            List<float> list2 = this.GetVisiblePoint(num5, 0f, base.CgInstance.GetWorkSpaceX(), (x2 - x1) * 2f);
            List<float> list3 = this.GetVisiblePoint(p, 0f, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID), (x2 - x1) * 2f);
            for (int i = 0; i < list2.Count; i++)
            {
                list.Add(new PointF(list2[i], 0f));
                list.Add(new PointF(list2[i] + (num6 - num5), (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID)));
            }
            for (int j = 0; j < list3.Count; j++)
            {
                list.Add(new PointF(0f, list3[j]));
                list.Add(new PointF(base.CgInstance.GetWorkSpaceX(), list3[j] + (num4 - p)));
            }
            List<float> list4 = this.GetVisiblePoint(num6, 0f, base.CgInstance.GetWorkSpaceX(), (x2 - x1) * 2f);
            List<float> list5 = this.GetVisiblePoint(num4, 0f, (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID), (x2 - x1) * 2f);
            for (int k = 0; k < list4.Count; k++)
            {
                if (!list2.Contains(list4[k] - (num6 - num5)))
                {
                    list.Add(new PointF(list4[k], (float) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID)));
                    list.Add(new PointF(list4[k] - (num6 - num5), 0f));
                }
            }
            for (int m = 0; m < list5.Count; m++)
            {
                if (!list3.Contains(list5[m] - (num4 - p)))
                {
                    list.Add(new PointF(base.CgInstance.GetWorkSpaceX(), list5[m]));
                    list.Add(new PointF(0f, list5[m] - (num4 - p)));
                }
            }
            return list;
        }

        public List<float> GetVisiblePoint(float p, float begin, float end, float step)
        {
            float item = p;
            List<float> list = new List<float>();
            if ((item >= begin) && (item <= end))
            {
                list.Add(item);
            }
            while (item >= begin)
            {
                item -= step;
                if (item >= begin)
                {
                    list.Add(item);
                }
            }
            item = p;
            while (item <= end)
            {
                item += step;
                if (item <= end)
                {
                    list.Add(item);
                }
            }
            list.Sort();
            return list;
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count <= 0)
            {
                return false;
            }
            int num2 = base.CgInstance.LastVisibleRecord - base.CgInstance.FirstVisibleRecord;
            int num3 = (base.CgInstance.FirstVisibleRecord - 1) + (num2 / 2);
            int num4 = (base.CgInstance.FirstVisibleRecord - 1) + ((num2 * 11) / 20);
            if (num3 == num4)
            {
                return false;
            }
            ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
            DataRow row = base.CgInstance.DataSource.Rows[num3];
            DataRow row2 = base.CgInstance.DataSource.Rows[num4];
            string key = row[base.CgInstance.CalenderScaleField].ToString();
            string str2 = row2[base.CgInstance.CalenderScaleField].ToString();
            base.PointList.Clear();
            double valueByPoint = base.CgInstance.GetValueByPoint(mp);
            base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
            base.PointList.Add(new PaintLineBase.PointValue(1, str2, valueByPoint));
            return true;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                PaintLineBase.PointValue value2 = base.PointList[0];
                double chartValue = value2.Value;
                PaintLineBase.PointValue value3 = base.PointList[1];
                double num1 = value3.Value;
                ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num3 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                float x = base.CgInstance.GetX(pos);
                float num5 = base.CgInstance.GetX(num3);
                float num6 = base.CgInstance.GetY(base.OwnedPanelID, chartValue, null);
                float num7 = num6 - (num5 - x);
                RectangleF ef = new RectangleF(x - (base.LineWidth * 2.5f), num6 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num5 - (base.LineWidth * 2.5f), num7 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint) || ef2.Contains((PointF) crossHairPoint))
                {
                    return true;
                }
                if (((((crossHairPoint.X - x) % (num5 - x)) >= (-3f * base.LineWidth)) && (((crossHairPoint.X - x) % (num5 - x)) <= (3f * base.LineWidth))) && ((((crossHairPoint.Y - num6) % (num5 - x)) >= (-3f * base.LineWidth)) && (((crossHairPoint.Y - num6) % (num5 - x)) <= (3f * base.LineWidth))))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    if ((mouseOverIndex + (num3 - pos)) <= (base.CgInstance.LastVisibleRecord - 1))
                    {
                        base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                        base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex + (num3 - pos)][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                        return true;
                    }
                }
                double[] param = Common.GetLineParam(x, num6, num5, num7);
                if (param != null)
                {
                    List<PointF> list = this.GetPointList(x, num6, num5, num7, param);
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (((i + 1) % 2) == 0)
                        {
                            PointF tf = list[i - 1];
                            PointF tf2 = list[i];
                            double[] numArray2 = Common.GetLineParam(tf.X, tf.Y, tf2.X, tf2.Y);
                            if (((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray2[0]) + numArray2[1])) >= 0.95) && ((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray2[0]) + numArray2[1])) <= 1.05))
                            {
                                return true;
                            }
                        }
                    }
                }
                param = Common.GetLineParam(x, num6, x - (num5 - x), num7);
                if (param != null)
                {
                    List<PointF> list2 = this.GetPointList(x, num6, num5, num7, param);
                    for (int j = 0; j < list2.Count; j++)
                    {
                        if (((j + 1) % 2) == 0)
                        {
                            PointF tf3 = list2[j - 1];
                            PointF tf4 = list2[j];
                            double[] numArray3 = Common.GetLineParam(tf3.X, tf3.Y, tf4.X, tf4.Y);
                            if (((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray3[0]) + numArray3[1])) >= 0.95) && ((((double) crossHairPoint.Y) / ((crossHairPoint.X * numArray3[0]) + numArray3[1])) <= 1.05))
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return null;
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    PaintLineBase.PointValue value2 = base.PointList[0];
                    int num3 = base.CgInstance.GetRecordByTime(value2.Key) - 1;
                    PaintLineBase.PointValue value3 = base.PointList[1];
                    int num4 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num5 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num6 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num7 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num8 = (num5 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num7 - num6);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num10 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            int num11 = (base.CgInstance.GetRecordByTime(value4.Key) + (num10 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            int num12 = (base.CgInstance.GetRecordByTime(value5.Key) + (num10 - indexByPoint)) - 1;
                            if (num11 >= 0)
                            {
                                if (num11 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num11 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num11 = 0;
                            }
                            if (num12 < 0)
                            {
                                num12 = 0;
                            }
                            else if (num12 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num12 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            if (num12 != num11)
                            {
                                PaintLineBase.PointValue value6 = base.BeginMoveValue[0];
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num11][base.CgInstance.CalenderScaleField].ToString(), value6.Value + num8);
                                PaintLineBase.PointValue value7 = base.BeginMoveValue[1];
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num12][base.CgInstance.CalenderScaleField].ToString(), value7.Value + num8);
                            }
                            return;
                        }
                        case 1:
                            if (mouseOverIndex < num4)
                            {
                                base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;

                        case 2:
                            if (mouseOverIndex > num3)
                            {
                                base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            }
                            return;
                    }
                }
            }
        }
    }
}

